package com.lujianfei.netease_android_shareelement.ui

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.app.ActivityOptionsCompat
import androidx.core.util.Pair
import androidx.recyclerview.widget.LinearLayoutManager
import com.lujianfei.netease_android_shareelement.R
import com.lujianfei.netease_android_shareelement.adapter.ShareElementAdapter
import com.lujianfei.netease_android_shareelement.beans.ListEntity
import kotlinx.android.synthetic.main.activity_share_element.*


class ShareElementActivity : AppCompatActivity() {

    private val adapter = ShareElementAdapter()
    private val list = arrayListOf<ListEntity>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_share_element)
        recyclerview.layoutManager = LinearLayoutManager(this)
        initData()
        adapter.onClickListener = { view, position ->
            val bean = list[position]
            startDetail(bean, view)
        }
    }

    private fun startDetail(bean: ListEntity, view: View) {
                ActivityCompat.startActivity(this, Intent(this, ShareElementDetailActivity::class.java)
                        .putExtra(ShareElementDetailActivity.KEY_DATA, bean),
                        ActivityOptionsCompat.makeSceneTransitionAnimation(this,
                                Pair(view.findViewById(R.id.icon), getString(R.string.transition_name_icon)),
                                Pair(view.findViewById(R.id.title), getString(R.string.transition_name_title))).toBundle())
    }

    private fun initData() {
        recyclerview.adapter = adapter
        for (index in 1..10) {
            list.add(ListEntity(R.mipmap.ic_launcher, "item$index"))
        }
        adapter.setData(list)
    }
}
